/* global  expect, describe, it*/

const fs = require('fs')
const apiTest = require('../apiTest')

// 帐号对象验证
var accountInfo = function(data) {
  apiTest.isObjectId(data._id)
  expect(data.status).to.equal(0)
  expect(data.username).to.be.an('string')
  if (data.mobile) expect(data.mobile).to.be.an('string')
  if (data.mail) expect(data.mail).to.be.an('string')
  if (data.role) {
    apiTest.isObjectId(data.role._id)
    expect(data.role.powers).to.be.an('array')
  }
}

describe('帐号登录', function() {
  it('登录 - 正常', function() {
    return apiTest.post('/account/login', { username: 'admin', password: '123123' })
      .then(function(res) {
      // cookie判断
      // var token = apiTest.getCookie(res).token
      // 数据判断
        var data = apiTest.apiOk(res)
        var token = res.data.token
        expect(token).to.be.a('string')
        fs.writeFileSync('test/token', token)
        accountInfo(data)
      })
  })

  it('登录 - 异常| 无参数', function() {
    return apiTest.post('/account/login', {})
      .then(function(res) {
        var error = apiTest.apiErr(res, 31000)
        expect(error).to.have.property('param')
      })
  })

  it('登录 - 异常| 错误用户名', function() {
    return apiTest.post('/account/login', { username: 'jdksjfklajlfsdjlk', password: 'test123' })
      .then(function(res) {
        var error = apiTest.apiErr(res, 32000)
        expect(error).to.have.property('param')
        expect(error.param).to.equal('username')
      })
  })

  it('登录 - 异常| 错误密码', function() {
    return apiTest.post('/account/login', { username: 'admin', password: 'testtest' })
      .then(function(res) {
        var error = apiTest.apiErr(res, 30005)
        expect(error).to.have.property('param')
        expect(error.param).to.equal('password')
      })
  })

  it('帐号信息 - 正常', function() {
    return apiTest.get('/account/info', {})
      .then(function(res) {
        var data = apiTest.apiOk(res)
        accountInfo(data)
      })
  })


  it('密码修改 - 正常', function() {
    return apiTest.post('/account/password', { old: '123123', password: '321321', password2: '321321' })
      .then(function(res) {
        var data = apiTest.apiOk(res)
        expect(data.n).to.equal(1)
        expect(data.ok).to.equal(1)
        // 恢复密码
        return apiTest.post('/account/password', { old: '321321', password: '123123', password2: '123123' })
          .then(function(res) {
            var data = apiTest.apiOk(res)
            expect(data.n).to.equal(1)
            expect(data.ok).to.equal(1)
          })
      })
  })

  it('密码修改 - 异常| 原密码为空', function() {
    return apiTest.post('/account/password', { old: '', password: '123123', password2: 'abcabc' })
      .then(function(res) {
        var error = apiTest.apiErr(res)
        expect(error.param).to.equal('old')
        expect(error.message).to.be.ok
      })
  })

  it('密码修改 - 异常| 新密码为空', function() {
    return apiTest.post('/account/password', { old: '123123', password: '', password2: 'abcabc' })
      .then(function(res) {
        var error = apiTest.apiErr(res)
        expect(error.param).to.equal('password')
        expect(error.message).to.be.ok
      })
  })

  it('密码修改 - 异常| 确认密码为空', function() {
    return apiTest.post('/account/password', { old: '123123', password: '123123', password2: '' })
      .then(function(res) {
        var error = apiTest.apiErr(res)
        expect(error.param).to.equal('password2')
        expect(error.message).to.be.ok
      })
  })

  it('密码修改 - 异常| 确认密码不一致', function() {
    return apiTest.post('/account/password', { old: '123123', password: '123123', password2: 'abcabc' })
      .then(function(res) {
        var error = apiTest.apiErr(res)
        expect(error.param).to.equal('password2')
        expect(error.message).to.be.ok
      })
  })

  it('密码修改 - 异常| 原密码错误', function() {
    return apiTest.post('/account/password', { old: 'xxxxxx', password: '123123', password2: '123123' })
      .then(function(res) {
        var error = apiTest.apiErr(res)
        expect(error.param).to.equal('old')
        expect(error.message).to.be.ok
      })
  })

  it('注销 - 正常', function() {
    return
    return apiTest.post('/account/logout', {}).then(function(res) {
      apiTest.apiOk(res)
    })
  })
})
